package com.lhx.product.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lhx.common.core.domain.R;
import com.lhx.product.domain.dto.ProductAttributeDTO;
import com.lhx.product.domain.po.ProductAttributePO;
import com.lhx.product.result.PageResult;
import com.lhx.product.domain.vo.ProductAttributeVO;

import com.lhx.product.service.ProductAttributeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author：李洪祥
 * @Package：com.lhx.product.controller
 * @Project：lhx-RuoYi
 * @name：ProductAttributeController
 * @Date：2024/5/25 上午9:26
 */
@RestController
@RequestMapping("/productAttribute")
@Api(tags = "商品属性管理模块")
public class ProductAttributeController {

    @Autowired
    private ProductAttributeService productAttributeService;

    /**
     * 查询商品属性列表
     * @param productAttributeDTO
     * @return
     */
    @ApiOperation(value = "分页查询商品属性列表")
    @PostMapping("/findList")
    public R<PageResult<ProductAttributeVO>> findList(@RequestBody ProductAttributeDTO productAttributeDTO) {
        List<ProductAttributeVO> list=productAttributeService.findList(productAttributeDTO);
        PageHelper.startPage(productAttributeDTO.getPageNum(),productAttributeDTO.getPageSize());
        return R.ok(PageResult.toPageResult(list.size(),list));
    }

    /**
     * 添加商品属性
     * @param
     * @return
     */
    @ApiOperation("根据分类查询属性列表或参数列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "type", value = "0表示属性，1表示参数", required = true, paramType = "query", dataType = "integer")})
    @GetMapping(value = "/list/{cid}")
    public R<PageResult<ProductAttributeVO>> getList(@PathVariable Long cid,
                                                               @RequestParam(value = "type") Integer type,
                                                               @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                               @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<ProductAttributeVO> productAttributeList = productAttributeService.getList(cid, type, pageSize, pageNum);
        PageInfo<ProductAttributeVO> objectPageInfo = new PageInfo<>(productAttributeList);
        return R.ok(PageResult.toPageResult(objectPageInfo.getTotal(),productAttributeList));
    }

    /**
     * 添加商品属性
     * @param productAttributeDTO
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody ProductAttributePO productAttributeDTO) {
        productAttributeService.add(productAttributeDTO);
        return R.ok(productAttributeDTO,"添加成功");
    }

    /**
     * 修改商品属性
     * @param productAttributeDTO
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody ProductAttributeDTO productAttributeDTO) {
        productAttributeService.update(productAttributeDTO);
        return R.ok(productAttributeDTO,"修改成功");
    }

    /**
     * 删除商品属性
     * @param ids
     * @return
     */

    @PostMapping("/delete")
    public R delete(@RequestBody Integer[] ids) {
        productAttributeService.delete(ids);
        return R.ok(ids,"删除成功");
    }
    /**
     * 根据id查询商品属性
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询商品属性")
    @PostMapping("findById")
    public R findById(@RequestParam Integer id) {
        ProductAttributeVO productAttributeVO=productAttributeService.findById(id);
        return R.ok(productAttributeVO,"查询成功");
    }
}
